package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Tag(name="01-资讯管理模块")
@RestController
@RequestMapping("v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("add")
    @Operation(summary = "添加资讯")
    @ApiOperationSupport(order = 10)
    @Parameters(value = {
            @Parameter(name = "title",description = "标题",required = true,example = "标题"),
            @Parameter(name = "content",description = "内容",required = true),
            @Parameter(name = "type",description = "类型",required = true,example = "1"),
            @Parameter(name = "status",description = "状态",required = true,example = "1"),
            @Parameter(name = "noticeAddParam",hidden = true)
    })
    public JsonResult addNotice(NoticeAddParam noticeAddParam) {
        log.debug("添加资讯："+noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        log.debug("准备插入数据库："+notice);
        noticeMapper.insertNotice(notice);
        return JsonResult.ok();
    }

    @GetMapping("list")
    @Operation(summary = "查询资讯列表")
    @ApiOperationSupport(order = 20)
    public JsonResult listNotice (NoticeListQuery noticeListQuery){
        log.debug("查询资讯："+noticeListQuery);
        List<NoticeListVO> noticeList = noticeMapper.selectNoticeList(noticeListQuery);
        return JsonResult.ok(noticeList);
    }

    @GetMapping("detail")
    @Operation(summary = "查询资讯详情")
    @ApiOperationSupport(order = 30)
    @Parameters(value = {
            @Parameter(name = "id",description = "资讯ID",required = true,example = "1"),
            @Parameter(name = "username",description = "用户名",required = true,example = "admin",hidden = true)
    })

    public JsonResult detailNotice (Integer id,String username){
        log.debug("查询资讯详情："+id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.selectNoticeDetailInfoById(id);
        return JsonResult.ok(noticeDetailInfoVO);
    }

    @DeleteMapping("del")
    @Operation(summary = "删除资讯")
    @ApiOperationSupport(order = 40)
    public JsonResult deleteNotice (Integer id){
        log.debug("删除资讯："+id);
        int rows = noticeMapper.deleteNoticeById(id);
        if(rows>0){
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    @PostMapping("update")
    @Operation(summary = "更新资讯")
    @ApiOperationSupport(order = 50)
    public JsonResult updateNotice(@RequestBody NoticeUpdateParam noticeUpdateParam){
        log.debug("更新资讯："+noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());
        log.debug("准备更新资讯："+notice);
        int rows =noticeMapper.updateNotice(notice);
        if(rows>0){
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
}
